#pragma once
#include "item.h"
#include <assert.h>

//remove the type defs... They are confusing

LOCAL_CONSTANT char * item_name[ITEM_MAX_TYPES];

void item_load()
{
	for (int i =0; i < ITEM_MAX_TYPES;i++)
	{
		item_name[i] = 0;
	}
	item_name[0] = "null item";
	item_name[1] = "wood_log";
}


const char * item_get_name(const Game * game,ITEM_TypeId_int item_type_id){
	return item_name[item_type_id];
}

int inventory_get_item_quantity(Inventory * inv, ItemType it)
{
	for (unsigned int i = 0; i < inv->_items.size(); i++){
		if (inv->_items.at(i) == (int)it){
			return inv->_item_quantity[i];
		}
	}
	return 0;
}

//add new stack if item full? // depends if item is multi stackable
void inventory_add_item(Inventory * inv, ItemType it, int _amount)
{
	ITEM_TypeId_int itemID = (int)it;
	assert(itemID >= 0 && itemID < ITEM_MAX_TYPES);
	assert(_amount > 0 && _amount < ITEM_MAX_QUANTITY);
	assert(inv);

	ITEM_Amount_int amount = (ITEM_Amount_int)_amount;
	bool added = false;

	for (unsigned int i = 0; i < inv->_items.size(); i++){
		if (inv->_items.at(i) == itemID){
			if(inv->_item_quantity[i] + amount < ITEM_MAX_QUANTITY)
			{
				inv->_item_quantity[i] += amount;
				added = true;
			}
			else
			{
				LOG_ERROR("max item quantity\n");
			}
			break;
		}
	}
	if (!added){
		inv->_items.push_back((ITEM_TypeId_int)itemID);
		inv->_item_quantity[inv->_items.size()-1]+=amount;
	}
}

//removes a single item
bool inventory_remove_any_item(Inventory * inv, ItemType & out_type)
{
	for (unsigned int item_type_index = 0; item_type_index < inv->_items.size(); item_type_index++)
	{
			if (inv->_item_quantity[item_type_index] > 0)
			{
				inv->_item_quantity[item_type_index] --;
				out_type = (ItemType)inv->_items[item_type_index];
				return true;
			}
	}
	out_type = (ItemType)0;
	return false;
}

bool inventory_remove_item(Inventory * inv, ItemType type_of_item, int _amount)
{
	assert(_amount > 0 && _amount < ITEM_MAX_QUANTITY);
	ITEM_Amount_int amount = (ITEM_Amount_int)_amount;

	for (unsigned int item_type_index = 0; item_type_index < inv->_items.size(); item_type_index++)
	{
		if (inv->_items[item_type_index] == type_of_item)
		{
			if (inv->_item_quantity[item_type_index] >= amount)
			{
				inv->_item_quantity[item_type_index] -= amount;
				return true;
			}
			else
			{
				printf("unable to remove item from entity");
				return false;
			}
			break;
		}
	}
	return false;
}
